﻿using System;
using System.IO;

namespace PMIS.BLL.System
{
	/// <summary>
	/// 日志管理
	/// </summary>
	public class BL_Logs
	{
		/// <summary>
		/// 日志文件完整路径名
		/// </summary>
		private static string strLogFileName
		{
			get
			{
				string strFileDirectory = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System)).ToString() + @"\Logs";
				try
				{
					//	删除5天前的日志
					string strLastFileName = string.Format("{0}\\{1}.log", strFileDirectory, DateTime.Now.AddDays(-5).ToString("yyyy-MM-dd"));
					if (File.Exists(strLastFileName))
						File.Delete(strLastFileName);

					//	没有日志目录就创建该目录
					if (!Directory.Exists(strFileDirectory))
						Directory.CreateDirectory(strFileDirectory);
				}
				catch
				{
					strFileDirectory = @"c:\Windows\Logs";
				}

				return string.Format("{0}\\{1}.log", strFileDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
			}
		}
		/// <summary>
		/// 将异常写入日志
		/// </summary>
		/// <param name="ex">异常</param>
		/// <returns>是否成功写入日志</returns>
		public static bool WriteLog(Exception ex)
		{
			if (ex == null || string.IsNullOrEmpty(ex.Message))
				return false;

			//	如果有StackTrace则将其写入日志
			return string.IsNullOrEmpty(ex.StackTrace) ?
				(ex.InnerException == null ?
					WriteLogText(string.Format("{0}\t{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message)) :
					WriteLogText(string.Format("{0}\t{1}\n{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message, ex.InnerException.Message))) :
				(ex.InnerException == null ?
					WriteLogText(string.Format("{0}\t{1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message, ex.StackTrace)) :
					WriteLogText(string.Format("{0}\t{1}\n{2}{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message, ex.InnerException.Message, ex.StackTrace)));
		}
		/// <summary>
		/// 将日志文本内容写入日志文件
		/// </summary>
		/// <param name="strContent">写入日志的文本内容</param>
		/// <returns>是否成功写入日志</returns>
		public static bool WriteLog(string strContent)
		{
			if (string.IsNullOrEmpty(strContent))
				return false;

			return WriteLogText(string.Format("{0}\t{1}",
				DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
				strContent));
		}
		/// <summary>
		/// 将日志文本内容按指定格式写入日志文件
		/// </summary>
		/// <param name="strContent">写入日志的文本内容</param>
		/// <returns>是否成功写入日志</returns>
		public static bool WriteLog(string strFormat, params object[] args)
		{
			return WriteLog(string.Format(strFormat, args));
		}
		/// <summary>
		/// 将日志文本内容和异常信息写入日志文件中
		/// </summary>
		/// <param name="strContent">写入日志的文本内容</param>
		/// <param name="ex">异常</param>
		/// <returns>是否成功写入日志</returns>
		public static bool WriteLog(string strContent, Exception ex)
		{
			if (string.IsNullOrEmpty(strContent) || ex == null || string.IsNullOrEmpty(ex.Message))
				return false;

			return string.IsNullOrEmpty(ex.StackTrace) ?
				WriteLogText(string.Format("{0}\t{1}\t{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), strContent, ex.Message)) :
				WriteLogText(string.Format("{0}\t{1}\t{2}\n{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), strContent, ex.Message, ex.StackTrace));
		}
		/// <summary>
		/// 将文本内容写入日志
		/// </summary>
		/// <param name="strLog">内容</param>
		/// <returns>是否写入成功</returns>
		private static bool WriteLogText(string strLog)
		{
			bool result = false;
			try
			{
				using (StreamWriter sw = File.AppendText(strLogFileName))
				{
					sw.WriteLine(strLog);
					//	写盘
					sw.Flush();
					sw.Close();
				}

				result = true;
			}
			catch
			{
				result = false;
			}

			return result;
		}
	}
}
